﻿var token = getCookie("token");
$.ajaxSetup({
    global: true,
    dataType: 'json',
    headers: {
        'Token': token
    }
});
function getCookie(name) {
    var strcookie = document.cookie;
    var arrcookie = strcookie.split("; ");
    for (var i = 0; i < arrcookie.length; i++) {
        var arr = arrcookie[i].split("=");
        if (arr[0] == name) return unescape(arr[1]);
    }
    return null;
}
//获取浏览器参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

/*窗体加载*/
$(function () {
    var t = getQueryString("t");
    if (t == 1) {
        // weiChat
        $('.eject_title').html('微信支付');
        $('.eject_code').html('微信动态码');
        $('#Text3').attr('placeholder', '微信动态码');
        $('#Text2').val(getQueryString('collection'));
        $('#Text4').val(getQueryString('money'));
    } else if (t == 2) {
        // bank
        $('.eject_title').text('银联卡支付');
        $('.eject_code').html('银联卡号');
        $('#Text3').attr('placeholder', '银联卡号');
        $('#Text2').val(getQueryString('collection'));
        $('#Text4').val(getQueryString('money'));
    } else if (t == 3) {
        // aliPay
        $('.eject_title').html('支付宝支付');
        $('.eject_code').html('支付宝动态码');
        $('#Text3').attr('placeholder', '支付宝动态码');
        $('#Text2').val(getQueryString('collection'));
        $('#Text4').val(getQueryString('money'));
    } else if (t == 5) {
        // bank
        $('.eject_title').text('银联卡退款');
        $('.eject_code').html('银联卡号');
        $('#Text3').attr('placeholder', '银联卡号');
        $('#Text2').val(getQueryString('collection'));
        $('#Text4').val(getQueryString('money'));
    }
});

$(function () {
    $('#Text3').focus();  //当前页面输入框选中

    //主页面快捷键
    $(document).on('keydown', function (e) {
        switch (e.keyCode) {
            case 27:   //Esc操作
                e.preventDefault();
                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
                parent.window.location.reload();
                break;

            case 13:    //回车
                e.preventDefault();
                var money = $('#Text2').val(), code = $('#Text3').val(), cardMoney = $('#Text4').val();
                if (!money) {
                    message('message.html?t=8', '300px', '200px');
                }
                else if (!code) {
                    message('message.html?t=10', '300px', '200px');
                }
                else {
                    var payTypeString = getQueryString("t"), payType, isRefund = 0;
                    if (payTypeString) {
                        if (payTypeString == '1') {
                            payType = 4;
                        } else if (payTypeString == '3') {
                            payType = 5;
                        } else if (payTypeString == '2') {
                            payType = 2;
                        } else if (payTypeString == '5') {
                            payType = 2;
                            isRefund = 1;
                        }
                    }
                    payments(money, cardMoney, code, payType, isRefund);
                }
                break;
        }
    });
});